<?php

namespace App\Http\Controllers\Admin\WarOrder;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

class WarOrderController extends BaseController
{
    /**
     * @api {get} war/order/season 赛季列表
     * @apiVersion 0.1.0
     * @apiName 赛季列表
     * @apiGroup 战令
     * @apiParam {string} name   赛季名称,非必传
     * @apiParam {numeric} page   分页默认1,非必传
     * @apiParam {numeric} perPage   分页记录数默认15,非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id": 1,
     * "name 赛季名称": "S1",
     * "time_from 赛季开始时间": "2020-12-01",
     * "time_to 赛季结束时间": "2024-03-03",
     * "advance_price 进阶战令价格单位金币": 6800
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 15,
     * "next": null,
     * "total": 1
     * }
     * }
     * }
     */
    public function season(Request $request)
    {
        $name = $request->input('name', '');
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 15);
        $where = [];
        if ($name) {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        $list = app('repo_war_order')->getSeasonList($where, $page, $perPage);

        return Response::sendList($list);
    }

    /**
     * @api {post} war/order/season 添加或修改赛季
     * @apiVersion 0.1.0
     * @apiName 添加或修改赛季
     * @apiGroup 战令
     * @apiParam {numeric} id   赛季id,非必传,如果是修改则必传,否则为添加
     * @apiParam {string} name   赛季名称,必传
     * @apiParam {numeric} advance_price   进阶价格单位金币,不能小于0的整数,必传
     * @apiParam {string} time_from   赛季开始时间,如2021-02-24 00:00:00,必传
     * @apiParam {string} time_to   赛季结束时间,如2021-03-24 23:59:59,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function addSeason(Request $request)
    {
        $name = $request->input('name');
        $advance_price = $request->input('advance_price');
        $time_from = $request->input('time_from');
        $time_to = $request->input('time_to');
        $name_length = mb_strlen($name);
        if ($name_length > 255 || $name_length < 1) {
            throw new BasicException(0, '赛季名称必须1-255个字符');
        }
        if (!is_numeric($advance_price) || floor($advance_price) != $advance_price || $advance_price < 0) {
            throw new BasicException(0, '进阶价格必须为不小于0的整数');
        }
        if (!$time_from) {
            throw new BasicException(0, '请选择开始时间');
        }
        if (!$time_to) {
            throw new BasicException(0, '请选择结束时间');
        }
        if ($time_from >= $time_to) {
            throw new BasicException(0, '结束时间必须大于开始时间');
        }
        app('repo_war_order')->addSeason($request->all());

        return Response::sendData('success');
    }

    /**
     * @api {get} war/order/season/detail 赛季战令列表
     * @apiVersion 0.1.0
     * @apiName 赛季战令列表
     * @apiGroup 战令
     * @apiParam {numeric} id   赛季id,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 34,
     * "war_order_id": 2,
     * "level": 1,
     * "price": 200,
     * "experience": 100,
     * "normal 普通奖励": [
     * {
     * "id": 64,
     * "item_id": 21,
     * "name": "积分",
     * "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png",
     * "number": 1
     * }
     * ],
     * "special 进阶奖励": [
     * {
     * "id": 68,
     * "item_id": 35,
     * "name": "金币",
     * "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/1bb80bc8bac8dd4f2c7fdfb5ea29b28fLyWWb.png",
     * "number": 2
     * }
     * ]
     * },
     * {
     * "id": 34,
     * "war_order_id": 2,
     * "level": 2,
     * "price": 200,
     * "experience": 100,
     * "normal": [
     * {
     * "id": 65,
     * "item_id": 21,
     * "name": "积分",
     * "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png",
     * "number": 1
     * }
     * ],
     * "special": []
     * }
     * ]
     * }
     */
    public function seasonDetail(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            throw new BasicException(0, '请选择赛季');
        }
        $detail = app('repo_war_order')->getWarOrderDetail($id);

        return Response::sendData($detail);
    }

    /**
     * @api {get} war/order/season/reward 赛季礼物列表
     * @apiVersion 0.1.0
     * @apiName 赛季礼物列表
     * @apiGroup 战令
     * @apiParam {numeric} id   赛季id,必传
     * @apiParam {numberic} recommend   是否宣传礼物1是0否,非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 21,
     * "detail_id": 21,
     * "war_order_id": 21,
     * "item_id": 71,
     * "name": "10级进阶奖励",
     * "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/d01e7c08b8abdd4c4314629aae5f47715zxZV.png",
     * "number": 1,
     * "type 1普通礼物2第一个进阶礼物3第二个进阶礼物": 3,
     * "recommend 1是0否": 1
     * }
     * ]
     * }
     */
    public function rewardList(Request $request)
    {
        $id = $request->input('id');
        $recommend = $request->input('recommend', -1);
        if (!$id) {
            throw new BasicException(0, '请选择赛季');
        }
        $where['war_order_id'] = $id;
        if (in_array($recommend, [0, 1])) {
            $where['recommend'] = $recommend;
        }
        $recommend = app('repo_war_order')->getRewardList($where);

        return Response::sendData($recommend);
    }

    /**
     * @api {post} war/order 添加修改战令
     * @apiVersion 0.1.0
     * @apiName 添加修改战令
     * @apiGroup 战令
     * @apiParam {numeric} id   战令id(如果不传id则添加,传id则修改),非必传
     * @apiParam {numberic} war_order_id   赛季id,必传
     * @apiParam {numberic} level   战令等级,必传,唯一
     * @apiParam {numberic} experience   升级战令所需经验,必传
     * @apiParam {numberic} price   购买战令所需金币,必传
     * @apiParam {array} rewards  战令奖励,非必传 {"type":1,"reward_id":21,"number":1}
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function addWarOrder(Request $request)
    {
        $params = $request->only(['id', 'war_order_id', 'level', 'experience', 'price', 'rewards']);
        if (!isset($params['war_order_id']) || !$params['war_order_id']) {
            throw new BasicException(0, '请选择赛季');
        }
        if (!isset($params['level']) || $params['level'] <= 0) {
            throw new BasicException(0, '等级有误');
        }
        if (!isset($params['experience']) || $params['experience'] <= 0) {
            throw new BasicException(0, '经验值有误');
        }
        if (!isset($params['price']) || $params['price'] <= 0) {
            throw new BasicException(0, '价格有误');
        }
        app('repo_war_order')->addWarOrder($params);

        return Response::sendData('success');
    }
    /**
     * @api {post} war/order/import 导入上期战令
     * @apiVersion 0.1.0
     * @apiName 导入上期战令
     * @apiGroup 战令
     * @apiParam {numberic} war_order_id   赛季id,必传
     *
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function importWarOrder(Request $request)
    {
        $war_order_id = $request->input('war_order_id', 0);
        if ($war_order_id <= 0) {
            throw new BasicException(0, '请选择赛季');
        }
        app('repo_war_order')->importWarOrder($war_order_id);

        return Response::sendData('success');
    }

    /**
     * @api {post} war/order/season/recommend 设置赛季宣传奖励
     * @apiVersion 0.1.0
     * @apiName 设置赛季宣传奖励
     * @apiGroup 战令
     * @apiParam {numeric} detail_id   战令id,必传
     * @apiParam {numeric} id   奖励id,必传
     * @apiParam {numberic} recommend   1设为宣传奖励0否,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     * }
     */
    public function updateRecommend(Request $request)
    {
        $params = $request->only(['detail_id', 'id', 'recommend']);

        if (!isset($params['detail_id']) || !$params['detail_id']) {
            throw new BasicException(0, '战令id有误');
        }
        if (!isset($params['id']) || !$params['id']) {
            throw new BasicException(0, '请选择奖励');
        }
        if (!isset($params['recommend']) || !in_array($params['recommend'], [0, 1])) {
            throw new BasicException(0, 'recommend只能为0或1');
        }
        $warOrderRe = app('model_war_order_reward')
            ->where('id', $params['id'])
            ->first();
        if (empty($warOrderRe)) {
            throw new BasicException(0, '没有该奖励');
        }
        $recommendNum = app('model_war_order_reward')
            ->where('war_order_id', $warOrderRe->war_order_id)
            ->where('recommend', 1)
            ->count();
        if ($recommendNum >= 3 && $params['recommend'] == 1) {
            throw new BasicException(0, '推荐为最多3个');
        }

        app('repo_war_order')->updateRecommend($params);

        return Response::sendData('success');
    }

    /**
     * @api {get} war/order/upgrade 用户战令升级记录
     * @apiVersion 0.1.0
     * @apiName 用户战令升级记录
     * @apiGroup 战令
     * @apiParam {numeric} user_code   用户编号,非必传
     * @apiParam {string} season_name   赛季名称,非必传
     * @apiParam {numeric} page
     * @apiParam {numeric} perPage
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id": 88,
     * "user_id": 642974,
     * "war_order_id 赛季id": 1,
     * "level_from": 25,
     * "level_to": 30,
     * "cost_gold 花费金币": 8000,
     * "type 1经验升级2购买等级": 2,
     * "add_time": 1614050265,
     * "user_data": {
     * "user_id": 642974,
     * "user_code": 679883,
     * "nickname": "瑞瑞瑞瑞瑞瑞瑞瑞瑞r",
     * "sex 1男2女": 2,
     * "head_picture": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/ios_images/2020-12-07/705B262C-DADC-4BC0-9B57-0635EC4BD655.jpeg"
     * },
     * "war_order": {
     * "id 赛季id": 1,
     * "name 赛季名称": "S1"
     * }
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": "1",
     * "next": "http://www.na.com/war/order/upgrade?=2",
     * "total": 83
     * }
     * }
     * }
     */
    public function upgradeList(Request $request)
    {
        $list = app('repo_war_order')->getUpgradeList($request);

        return Response::sendList($list);
    }

    /**
     * @api {get} war/order/order 进阶购买记录
     * @apiVersion 0.1.0
     * @apiName 进阶购买记录
     * @apiGroup 战令
     * @apiParam {numeric} user_code   用户编号,非必传
     * @apiParam {string} season_name   赛季名称,非必传
     * @apiParam {numeric} page
     * @apiParam {numeric} perPage
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id": 11,
     * "user_id": 642974,
     * "war_order_id": 1,
     * "cost_gold": 6800,
     * "add_time": 1614050041,
     * "user_data": {
     * "user_id": 642974,
     * "user_code": 679883,
     * "nickname": "瑞瑞瑞瑞瑞瑞瑞瑞瑞r",
     * "sex": 2,
     * "head_picture": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/ios_images/2020-12-07/705B262C-DADC-4BC0-9B57-0635EC4BD655.jpeg"
     * },
     * "war_order": {
     * "id": 1,
     * "name": "S1"
     * }
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": "1",
     * "next": "http://www.na.com/war/order/order?=2",
     * "total": 11
     * }
     * }
     * }
     */
    public function order(Request $request)
    {
        $list = app('repo_war_order')->getOrderList($request);

        return Response::sendList($list);
    }

    /**
     * @api {delete} war/order/season 删除赛季
     * @apiVersion 0.1.0
     * @apiName 删除赛季
     * @apiGroup 战令
     * @apiParam {numeric} id   赛季id,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     */
    public function deleteSeason(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            throw new BasicException(0, '请选择赛季');
        }
        $detail = app('model_war_order')->where('id', $id)->first();
        if (!$detail) {
            throw new BasicException(0, '赛季不存在');
        }
        app('model_war_order')->where('id', $id)->delete();

        return Response::sendData('success');
    }

    /**
     * @api {delete} war/order 删除战令
     * @apiVersion 0.1.0
     * @apiName 删除战令
     * @apiGroup 战令
     * @apiParam {numeric} id   战令id,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": "success"
     */
    public function deleteWarOrder(Request $request)
    {
        $id = $request->input('id');
        if (!$id) {
            throw new BasicException(0, '请选择战令');
        }
        $detail = app('model_war_order_detail')->where('id', $id)->first();
        if (!$detail) {
            throw new BasicException(0, '战令不存在');
        }
        app('model_war_order_detail')->where('id', $id)->delete();
        app('model_war_order_reward')->where('detail_id', $id)->delete();

        return Response::sendData('success');
    }
}
